package org.cjstudio.messageboard;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class MessageFormServlet extends HttpServlet {

	public static final String pn_operation = "operation";
	
	// Branch the request to CRUD operation
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		
		MessageServlet messageServlet = new MessageServlet();
		
		int operation = MessageCommands.getOpcodeFromString(req.getParameter(pn_operation));
		switch(operation) {
			case MessageCommands.OPCODE_CREATE:
				messageServlet.doPost(req, resp);
				
				break;
			case MessageCommands.OPCODE_RETRIEVE:
				messageServlet.doGet(req, resp);
				
				break;
			case MessageCommands.OPCODE_UPDATE:
				messageServlet.doPut(req, resp);
				
				break;
			case MessageCommands.OPCODE_DELETE:
				messageServlet.doDelete(req, resp);
				
				break;
			default:
				break;
		}
		
		resp.sendRedirect("/messageBoard");
	}
}
